package com.tarena.lbs.activity.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.activity.dao.mapper.ActivityMapper;
import com.tarena.lbs.activity.dao.repository.ActivityRepository;
import com.tarena.lbs.activity.pojo.po.ActivityPO;
import com.tarena.lbs.activity.pojo.query.ActivityQuery;
import com.tarena.lbs.activity.pojo.vo.ActivityVO;
import com.tarena.lbs.user.api.UserApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Repository
public class ActivityRepositoryImpl implements ActivityRepository {

    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public List<ActivityVO> pageList(ActivityQuery query) {
        log.info("商家活动列表入参{}", query);
        QueryWrapper<ActivityPO> queryWrapper = new QueryWrapper<>();
        // 根据活动名称模糊查询
        if (StringUtils.hasText(query.getActivityName())) {
            queryWrapper.like("activity_name", query.getActivityName());
        }

        // 根据投放渠道查询
        if (query.getDeliveryChannel() != null) {
            queryWrapper.eq("delivery_channel", query.getDeliveryChannel());
        }

        // 根据活动类型查询
        if (query.getType() != null) {
            queryWrapper.eq("activity_type", query.getType());
        }

        // 根据活动渠道查询
        if (query.getChannelType() != null) {
            queryWrapper.eq("channel_type", query.getChannelType());
        }

        // 根据活动状态查询
        if (query.getStatus() != null) {
            queryWrapper.eq("status", query.getStatus());
        }

        // 根据商家ID查询
        if (query.getBusinessId() != null) {
            queryWrapper.eq("business_id", query.getBusinessId());
        }

        if (StringUtils.hasText(query.getStartDate()) && StringUtils.hasText(query.getEndDate())) {
            // 查询活动时间区间与给定时间区间有重叠的活动
            queryWrapper.le("start_date", query.getEndDate())     // 活动开始时间 <= 查询结束时间
                    .ge("end_date", query.getStartDate());
        }
            // 按创建时间倒序排列
        queryWrapper.orderByDesc("create_at");

        List<ActivityPO> activityPOS = activityMapper.selectList(queryWrapper);

        List<ActivityVO> activityVOS = activityPOS.stream().map(po -> {
            ActivityVO activityVO = new ActivityVO();
            BeanUtils.copyProperties(po, activityVO);
            return activityVO;
        }).collect(Collectors.toList());
        return activityVOS;
    }

    @Override
    public ActivityVO activityDetail(Integer id) {
        // TODO: 实现详情查询逻辑
        return null;
    }

    @Override
    public void activityDelete(Integer id) {
        // TODO: 实现删除逻辑
    }

    @Override
    public void save(ActivityPO activityPO) {
        log.info("准备保存活动数据: {}", activityPO);
        try {
            if (activityPO.getId() != null) {
                // 更新操作
                log.info("执行更新操作，ID: {}", activityPO.getId());
                int result = activityMapper.updateById(activityPO);
                log.info("更新操作结果: {}", result);
            } else {
                // 新增操作
                log.info("执行新增操作");
                int result = activityMapper.insert(activityPO);
                log.info("新增操作结果: {}", result);
            }
        } catch (Exception e) {
            log.error("保存活动数据时发生异常: ", e);
            throw e;
        }
    }
}